import Vue from 'vue'
import VueRouter from 'vue-router'
import auth from "@/api/users/auth";

Vue.use(VueRouter)

// 用户身份标识
const userIdentity = {
    STUDENT: 0,             // 学生
    TEACHER: 100,           // 教师
    COLLEGE_ADMIN: 200,     // 学院教务
    UNIVERSITY_ADMIN: 500,  // 学校教务
    SUPER_USER: 999         // 超级管理员
}

const routerMap = {
    0: "student",
    100: "teacher",
    200: "collegeAdmin",
    500: "schoolAdmin",
    999: "super"
}

const routes = [
    {
        path: "",
        name: "index",
        component: () => import('../pages/index'),
        redirect: "/user/login",
        children: [
            {
                path: "student",
                name: "student",
                component: () => import('../pages/student'),
                redirect: "/student/courseSearch",
                children: [
                    {
                        path: "courseSearch",
                        name: "courseSearch",
                        component: () => import('../pages/student/courseSearch'),
                        meta: {
                            title: "我的课表",
                            requireAuth: true,
                            requireIdentity: userIdentity.STUDENT
                        }
                    },
                    {
                        path: "courseSelect",
                        name: "courseSelect",
                        component: () => import('../pages/student/courseSelect'),
                        meta: {
                            title: "选课中心",
                            requireAuth: true,
                            requireIdentity: userIdentity.STUDENT
                        }
                    },
                    {
                        path: "scoreQuery",
                        name: "scoreQuery",
                        component: () => import('../pages/student/scoreQuery'),
                        meta: {
                            title: "我的成绩",
                            requireAuth: true,
                            requireIdentity: userIdentity.STUDENT
                        }
                    },
                    {
                        path: "*",
                        name: "studentAll",
                        redirect: "/student/courseSearch"
                    }
                ]
            },
            {
                path: "schoolAdmin",
                name: "schoolAdmin",
                component: () => import("../pages/schoolAdmin"),
                redirect: "/schoolAdmin/studentManagement",
                children: [
                    {
                        path: "studentManagement",
                        name: "studentManagement",
                        component: () => import("../pages/schoolAdmin/studentManagement"),
                        meta: {
                            title: "学生管理",
                            requireAuth: true,
                            requireIdentity: userIdentity.UNIVERSITY_ADMIN
                        }
                    },
                    {
                        path: "adminManagement",
                        name: "adminManagement",
                        component: () => import("../pages/schoolAdmin/adminManagement"),
                        meta: {
                            title: "教务指定",
                            requireAuth: true,
                            requireIdentity: userIdentity.UNIVERSITY_ADMIN
                        }
                    },
                    {
                        path: "trainingPlanManagement",
                        name: "trainingPlanManagement",
                        component: () => import("../pages/schoolAdmin/trainingPlanManagement"),
                        meta: {
                            title: "培养计划管理",
                            requireAuth: true,
                            requireIdentity: userIdentity.UNIVERSITY_ADMIN
                        }
                    },
                    {
                        path: "*",
                        name: "schoolAdminAll",
                        redirect: "/schoolAdmin/studentManagement"
                    }
                ],
            },
            {
                path: "teacher",
                name: "teacher",
                component: () => import("../pages/teacher"),
                redirect: "/teacher/courseTable",
                children: [
                    {
                        path: "courseTable",
                        name: "courseTable",
                        component: () => import("../pages/teacher/courseTable"),
                        meta: {
                            title: "我的课表",
                            requireAuth: true,
                            requireIdentity: userIdentity.TEACHER
                        }
                    }, {
                        path: "course",
                        name: "teacherCourse",
                        component: () => import("../pages/teacher/course"),
                        meta: {
                            title: "我的课程",
                            requireAuth: true,
                            requireIdentity: userIdentity.TEACHER
                        }
                    }, {
                        path: "applications",
                        name: "teacherApplication",
                        component: () => import("../pages/teacher/applications"),
                        meta: {
                            title: "申请状态",
                            requireAuth: true,
                            requireIdentity: userIdentity.TEACHER
                        }
                    }, {
                        path: "*",
                        name: "teacherAll",
                        redirect: "/teacher/courseTable"
                    }
                ]
            },
            {
                path: "courseDetail",
                name: "courseDetail",
                component: () => import("../pages/classInfo/courseDetail"),
                meta: {
                    title: "课程详情",
                    requireAuth: true,
                    requireIdentity: userIdentity.TEACHER
                },
            },
        ],
    },
    {
        path: "/user",
        name: "user",
        redirect: "/user/login",
        component: () => import("../pages/login/login"),
        children: [
            {
                path: "login",
                name: "login",
                component: () => import("../pages/login/loginTab")
            }
        ]
    },
    {
        path: "*",
        name: "all",
        redirect: "/student/courseSearch"
    }
]

const router = new VueRouter({
    mode: "history",
    routes
})

router.beforeEach(async (to, from, next) => {
    let title = "";
    if (to.meta.title) {
        title = "| " + to.meta.title;
    }
    document.title = "教务管理系统" + title;
    let authRes;
    if (to.name === "login") {
        authRes = await auth();
        // 登录成功之后不再显示登录页面
        if (authRes.result) {
            let identity = authRes.identity - 0;
            next({
                name: routerMap[identity]
            });
        }
    }
    if (to.meta.requireAuth) {
        authRes = await auth();
        if (authRes.result) {
            if (authRes.identity == to.meta.requireIdentity) {
                next();
            } else {
                // 权限不足时清除登录信息
                localStorage.clear();
                next({
                    name: 'login'
                })
            }
        } else {
            localStorage.clear();
            next({
                name: 'login'
            });
        }
    } else {
        next();
    }
});

export default router